package com.alogic.json;

import com.anysoft.util.Configurable;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.spi.Mode;
import com.jayway.jsonpath.spi.impl.AbstractJsonProvider;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONStyle;
import net.minidev.json.parser.ContainerFactory;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import java.util.Map;

/**
 * JsonSmartJsonProvider实现
 */
public class JsonSmartJsonProvider extends AbstractJsonProvider implements Configurable {
    private Mode mode = Mode.SLACK;
    private JSONStyle compression = JSONStyle.NO_COMPRESS;
    private ContainerFactory containerFactory = ContainerFactory.FACTORY_SIMPLE;

    public JsonSmartJsonProvider() {
    }

    @Override
    public void configure(Properties p){
        int flag = 0;
        if (PropertiesConstants.getBoolean(p,"http.json.protectKeys",false)){
            flag |= JSONStyle.FLAG_PROTECT_KEYS;
        }
        if (PropertiesConstants.getBoolean(p,"http.json.protectValues",false)){
            flag |= JSONStyle.FLAG_PROTECT_VALUES;
        }
        if (PropertiesConstants.getBoolean(p,"http.json.protect4Web",true)){
            flag |= JSONStyle.FLAG_PROTECT_4WEB;
        }
        if (PropertiesConstants.getBoolean(p,"http.json.agressive",false)){
            flag |= JSONStyle.FLAG_AGRESSIVE;
        }
        compression = new JSONStyle(flag);
    }

    @Override
    public Map<String, Object> createMap() {
        return containerFactory.createObjectContainer();
    }

    @Override
    public List<Object> createArray() {
        return containerFactory.createArrayContainer();
    }

    @Override
    public Object parse(String json) {
        try {
            return createParser().parse(json, containerFactory);
        } catch (ParseException e) {
            throw new InvalidJsonException(e);
        }
    }

    @Override
    public Object parse(Reader jsonReader) throws InvalidJsonException {
        try {
            return createParser().parse(jsonReader, containerFactory);
        } catch (ParseException e) {
            throw new InvalidJsonException(e);
        }
    }

    @Override
    public Object parse(InputStream jsonStream) throws InvalidJsonException {
        try {
            return createParser().parse(new InputStreamReader(jsonStream), containerFactory);
        } catch (ParseException e) {
            throw new InvalidJsonException(e);
        }
    }

    @Override
    public String toJson(Object obj) {
        if(obj instanceof Map) {
            return JSONObject.toJSONString((Map<String, ?>) obj,compression);
        } else if(obj instanceof List){
            return JSONArray.toJSONString((List<?>) obj,compression);
        } else {
            throw new UnsupportedOperationException(obj.getClass().getName() + " can not be converted to JSON");
        }
    }

    @Override
    public Mode getMode() {
        return mode;
    }

    private JSONParser createParser(){
        return new JSONParser(mode.intValue());
    }
}
